package list_;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author wly
 * @version V1.0.0
 * @date 2022/12/30 21:33
 */
public class ListExercise02 {
    public static void main(String[] args) {
        List list = new ArrayList();
        list.add(new Book("红楼梦", "曹雪芹", 100));
        list.add(new Book("西游记", "吴承恩", 10));
        list.add(new Book("水浒传", "施耐庵", 19));
        list.add(new Book("三国", "罗贯中", 80));
//        System.out.println(list);
        Iterator iterator = list.iterator();
        while (iterator.hasNext()) {
            Object obj = iterator.next();
            System.out.println(obj);
        }

        sort(list);

        System.out.println("==排序后==");

        for (Object o : list) {
            System.out.println(o);
        }
    }

    public static void sort(List list) {
        int len = list.size();
        for (int i = 0; i < len - 1; i++) {
            for (int j = 0; j < len - 1 - i; j++) {
                Book book1 = (Book) (list.get(j));
                Book book2 = (Book) (list.get(j + 1));
                if (book1.getPrice() > book2.getPrice()) {
                    list.set(j, book2);
                    list.set(j + 1, book1);
                }
            }
        }
    }
}
